package com.rt.easyjava.common.dozer;

import org.dozer.BeanFactory;
import org.dozer.CustomConverter;
import org.dozer.DozerEventListener;
import org.dozer.Mapper;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.core.io.Resource;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by wxy on 2017/9/14.
 */
public class DozerBeanMapperFactoryBeanPlus<T1,T2> implements FactoryBean, InitializingBean, DisposableBean {

    DozerBeenMapperPlus beanMapper;
    private Resource[] mappingFiles;
    private List<CustomConverter> customConverters;
    private Map<String, CustomConverter> customConvertersWithId;
    private List<DozerEventListener> eventListeners;
    private Map<String, BeanFactory> factories;

    public DozerBeanMapperFactoryBeanPlus() {
    }

    public final void setMappingFiles(Resource[] mappingFiles) {
        this.mappingFiles = mappingFiles;
    }

    public final void setCustomConverters(List<CustomConverter> customConverters) {
        this.customConverters = customConverters;
    }

    public void setCustomConvertersWithId(Map<String, CustomConverter> customConvertersWithId) {
        this.customConvertersWithId = customConvertersWithId;
    }

    public final void setEventListeners(List<DozerEventListener> eventListeners) {
        this.eventListeners = eventListeners;
    }

    public final void setFactories(Map<String, BeanFactory> factories) {
        this.factories = factories;
    }

    public final Object getObject() throws Exception {
        return this.beanMapper;
    }

    public final Class<Mapper> getObjectType() {
        return Mapper.class;
    }

    public final boolean isSingleton() {
        return true;
    }

    public final void afterPropertiesSet() throws Exception {
        this.beanMapper = new DozerBeenMapperPlus();
        if (this.mappingFiles != null) {
            List<String> mappings = new ArrayList(this.mappingFiles.length);
            Resource[] arr$ = this.mappingFiles;
            int len$ = arr$.length;

            for(int i$ = 0; i$ < len$; ++i$) {
                Resource mappingFile = arr$[i$];
                mappings.add(mappingFile.getURL().toString());
            }

            this.beanMapper.setMappingFiles(mappings);
        }

        if (this.customConverters != null) {
            this.beanMapper.setCustomConverters(this.customConverters);
        }

        if (this.customConvertersWithId != null) {
            this.beanMapper.setCustomConvertersWithId(this.customConvertersWithId);
        }

        if (this.eventListeners != null) {
            this.beanMapper.setEventListeners(this.eventListeners);
        }

        if (this.factories != null) {
            this.beanMapper.setFactories(this.factories);
        }

    }

    public void destroy() throws Exception {
        if (this.beanMapper != null) {
            this.beanMapper.destroy();
        }

    }
}
